//
//  GXiaNetHandle.m
//  AFN0818网络封装
//
//  Created by 甘霞 on 15/10/20.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXiaNetHandle.h"
#import "AFNetworking.h"

@implementation GXiaNetHandle

//GET请求
+ (void)getUrl:(NSString *)url cookie:(NSString *)cookie success:(void (^)(id result))success
       failure:(void (^)(NSError *error))failure {
    
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/json", @"text/html", @"application/json", nil]];
    //接口带 cookie
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    [manager.requestSerializer setValue:@"c6d5822e76c811e596e85254007d4448" forHTTPHeaderField:@"HTTP-AUTHORIZATION"];
    
    [manager GET:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (responseObject) {
            success(responseObject);
            //缓存
            NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];
            
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *sandBoxPath = sandBox[0];
            NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
//            NSLog(@"%@", caches);
            [NSKeyedArchiver archiveRootObject:responseObject toFile:caches];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (error) {
            //失败回调  缓存
            failure(error);
            
            NSString *str = [url stringByReplacingOccurrencesOfString:@"/" withString:@""];
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            
            NSString *sandBoxPath = sandBox[0];
            NSString *caches = [sandBoxPath stringByAppendingPathComponent:str];
            
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:caches];
            if (responseObject != nil) {
                success(responseObject);
            }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        }
    }];
}

//POST 请求
+ (void)postUrl:(NSString *)url parameters:(id)parameter result:(JSONBLOCK)block {
    //有 cookie 自行加 cookie
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        block(responseObject);
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

@end
